package com.wlts.wzy.LMGoodsModule.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.linkedmall.model.v20180116.QueryItemDetailResponse;
import com.aliyuncs.linkedmall.model.v20180116.QueryItemInventoryRequest;
import com.aliyuncs.linkedmall.model.v20180116.QueryItemInventoryResponse;
import com.wlts.wzy.Base.BaseEntity;
import com.wlts.wzy.LMGoodsModule.entity.LmGoods;
import com.wlts.wzy.LMGoodsModule.entity.LmGoodsDetails;
import com.wlts.wzy.LMGoodsModule.service.ILmGoodsService;
import com.wlts.wzy.Result.Result;
import com.wlts.wzy.api.request.lm.GetLmGoodsInventoryRequest;
import com.wlts.wzy.api.request.lm.PageLmGoodsRequest;
import com.wlts.wzy.configBean.LmConfig.LmConfig;
import com.wlts.wzy.expand.MyLambdaQueryWrapper;
import com.wlts.wzy.expand.MyPage;
import com.wlts.wzy.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * LM商品表 前端控制器
 * </p>
 *
 * @author zhiyong
 * @since 2021-01-29
 */
@Api(tags = "LM商品表 前端控制器")
@RestController
@RequestMapping("/LMGoodsModule/lm-goods")
public class LmGoodsController {
    @Autowired
    private ILmGoodsService lmGoodsService;
    private static final IAcsClient client = LmConfig.client;
    @ApiOperation(value = "分页获得商品库")
    @RequestMapping(value = "getPage", method = RequestMethod.POST)
    public Result<MyPage<LmGoods>> getPage(@RequestBody PageLmGoodsRequest request) {



        MyLambdaQueryWrapper<LmGoods> wrapper = new MyLambdaQueryWrapper<>();

        if (StringUtils.isNotEmpty(request.getItemTitle())) {
            wrapper.like(LmGoods::getItemTitle, request.getItemTitle());
        }

        if (StringUtils.isNotEmpty(request.getItemId())) {
            wrapper.eq(LmGoods::getItemId, request.getItemId());
        }

        wrapper.orderByDesc(BaseEntity::getCreateDate);

        MyPage<LmGoods> lmSelfGoodsList = lmGoodsService.page(wrapper, request.getCurrentPage(), request.getPageSize(), true);

        return Result.buildResult(lmSelfGoodsList);
    }

    @ApiOperation(value = "修改LM详细图片列表")
    @RequestMapping(value = "changeDetailsImages", method = RequestMethod.POST)
    public Result<LmGoods> changeDetailsImages(@RequestBody LmGoods lmGoods) {
        lmGoodsService.updateById(lmGoods);
        return Result.buildResult(lmGoods);
    }


    /**
     * 暂时不用
     */
    @ApiOperation(value = "获得商品库存")
    @RequestMapping(value = "GetLmGoodsInventory", method = RequestMethod.POST)
    public Result GetLmGoodsInventory(@RequestBody GetLmGoodsInventoryRequest request) {

        QueryItemInventoryRequest queryItemInventoryRequest = new QueryItemInventoryRequest();
        queryItemInventoryRequest.setBizId(LmConfig.LZ_BIZ_ID);


        List<QueryItemInventoryRequest.ItemList> itemListList = new ArrayList<>();

        QueryItemInventoryRequest.ItemList itemList = new QueryItemInventoryRequest.ItemList();
        itemList.setItemId(request.getItemId());
        itemList.setSkuIdLists(request.getSkuList());
        itemListList.add(itemList);
        queryItemInventoryRequest.setItemLists(itemListList);
        queryItemInventoryRequest.setDivisionCode(request.getDivisionCode());

        try {
            QueryItemInventoryResponse response = client.getAcsResponse(queryItemInventoryRequest);
            System.out.println(JSON.toJSONString(response.getItemList()));
            return Result.buildResult(response);
        } catch (ClientException e) {
            e.printStackTrace();
        }

        return Result.buildResult(500,"获取失败");

    }

}
